/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.activity.home.fragment.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.activity.webview.BrowserActivity;
import com.dataoke.shoppingguide.app773026.bean.haodanku.HaodankuIndexTileShortBean;
import com.dataoke.shoppingguide.app773026.util.GlideUtils;
import com.dataoke.shoppingguide.app773026.util.HaoDanKuUtil;

import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class TileShortAdapter extends RecyclerView.Adapter<TileShortAdapter.ViewHolder>{
    private final Context mContext;
    private final List<HaodankuIndexTileShortBean> beans;

    LayoutInflater mLayoutInflater;

    public TileShortAdapter(Context mContext, List<HaodankuIndexTileShortBean> beans) {
        this.mContext = mContext;
        this.beans = beans;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mTileShort;

        public ViewHolder(View inflate) {
            super(inflate);
            mTileShort = inflate.findViewById(R.id.iv_tile_short);
            mTileShort.setOnClickListener(v -> {
                int index = getBindingAdapterPosition();
                HaodankuIndexTileShortBean bean = beans.get(index);
                BrowserActivity.show(mContext, HaoDanKuUtil.getUrl(bean), bean.getTitle());
            });
        }
    }


    /**
     * 根据视图类型创建ViewHolder
     * @param parent
     * @param viewType 当前的视图类型
     * @return RecyclerView.ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_tile_short, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //根据位置得到对应的数据
        HaodankuIndexTileShortBean channelInfoBean = beans.get(position);
        //根据Glide设置图片
        GlideUtils.loadTileShortCrop(mContext, channelInfoBean.getImg(), holder.mTileShort);
    }

    /**
     * 总共有多少个item
     * @return
     */
    @Override
    public int getItemCount() {
        return beans.size();
    }
}
